package cn.wolfcode.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.wolfcode.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.BusServiceItemMapper;
import cn.wolfcode.business.domain.BusServiceItem;
import cn.wolfcode.business.service.IBusServiceItemService;

/**
 * 服务项Service业务层处理
 *
 * @author wolfcode
 * @date 2025-09-19
 */
@Service
public class BusServiceItemServiceImpl implements IBusServiceItemService {
    @Autowired
    private BusServiceItemMapper busServiceItemMapper;

    /**
     * 查询服务项
     *
     * @param id 服务项主键
     * @return 服务项
     */
    @Override
    public BusServiceItem selectBusServiceItemById(Long id) {
        return busServiceItemMapper.selectBusServiceItemById(id);
    }

    /**
     * 查询服务项列表
     *
     * @param busServiceItem 服务项
     * @return 服务项
     */
    @Override
    public List<BusServiceItem> selectBusServiceItemList(BusServiceItem busServiceItem) {
        return busServiceItemMapper.selectBusServiceItemList(busServiceItem);
    }

    /**
     * 新增服务项
     *
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int insertBusServiceItem(BusServiceItem busServiceItem) {
        //1.参数合理化校验
        if (busServiceItem == null) {
            throw new RuntimeException("参数异常");
        }
        if (busServiceItem.getName() == null || busServiceItem.getOriginalPrice() == null ||
                busServiceItem.getDiscountPrice() == null || busServiceItem.getCarPackage() == null ||
                busServiceItem.getServiceCatalog() == null || busServiceItem.getInfo() == null) {
            throw new RuntimeException("参数异常");
        }
        //价格合理化验证
        //原价大于等于0，且折扣大于等于0，，且折扣价格小于等于原价
        //BigDecimal运算符不能使用 > >= < <= 等
        //xx.compareTo(xx) 调用者 > 参数，返回正数1 ，调用者=参数 返回0，调用者<参数返回-1
        if (!(busServiceItem.getOriginalPrice().compareTo(BigDecimal.ZERO) >= 0
                && busServiceItem.getDiscountPrice().compareTo(BigDecimal.ZERO) >= 0
                && busServiceItem.getOriginalPrice().compareTo(busServiceItem.getDiscountPrice()) >= 0)) {
            throw new RuntimeException("原价折扣价需大于0且原价需要大于折扣价");

        }
        //3.判断插入数据是否为套餐
        if (busServiceItem.CARPACKAGE_NO.equals(busServiceItem.getCarPackage())) {
            // 不是套餐 初始化状态变为4 ——无需审核
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_NO_REQUIRED);
        } else {
            //是套餐 状态变为0——初始化
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_INIT);
            // 提高代码安全性设置，将默认的上下架装丰台设置为下架状态
            busServiceItem.setSaleStatus(BusServiceItem.SALESTATUS_OFF);
        }
        busServiceItem.setCreateTime(DateUtils.getNowDate());
        return busServiceItemMapper.insertBusServiceItem(busServiceItem);
    }

    /**
     * 修改服务项
     *
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int updateBusServiceItem(BusServiceItem busServiceItem) {
        //1.参数合理化校验
        if (busServiceItem == null) {
            throw new RuntimeException("参数异常");
        }
        if (busServiceItem.getName() == null || busServiceItem.getOriginalPrice() == null ||
                busServiceItem.getDiscountPrice() == null || busServiceItem.getCarPackage() == null ||
                busServiceItem.getServiceCatalog() == null || busServiceItem.getInfo() == null) {
            throw new RuntimeException("非法参数");
        }

        //价格合理化验证
        if (!(busServiceItem.getOriginalPrice().compareTo(BigDecimal.ZERO) >= 0
                && busServiceItem.getDiscountPrice().compareTo(BigDecimal.ZERO) >= 0
                && busServiceItem.getOriginalPrice().compareTo(busServiceItem.getDiscountPrice()) >= 0)) {
            throw new RuntimeException("原价折扣价需大于0且原价需要大于折扣价");
        }

        //3.验证是否套餐禁止修改
        BusServiceItem oldObj = this.selectBusServiceItemById(busServiceItem.getId());
        if (oldObj == null || oldObj.getCarPackage() == null || oldObj.getAuditStatus() == null) {
            throw new RuntimeException("参数错误");
        }

        //验证 如果这个套餐不等于数据库查出的套餐
        if (!oldObj.getCarPackage().equals(busServiceItem.getCarPackage())) {
            throw new RuntimeException("套餐不允许修改");
        }

        //上架状态禁止修改
        if (!oldObj.getSaleStatus().equals(busServiceItem.getSaleStatus())
                || BusServiceItem.SALESTATUS_ON.equals(busServiceItem.getSaleStatus())) {
            throw new RuntimeException("修改的数据必须是下架状态且上下架状态禁止修改");
        }

        //验证审批状态
        if (!oldObj.getAuditStatus().equals(busServiceItem.getAuditStatus())) {
            throw new RuntimeException("审批状态不允许修改");
        }

        if (BusServiceItem.CARPACKAGE_NO.equals(busServiceItem.getCarPackage())
                && BusServiceItem.AUDITSTATUS_AUDITING.equals(busServiceItem.getAuditStatus())) {
            throw new RuntimeException("审批中套餐禁止修改");
        }

        if (BusServiceItem.CARPACKAGE_NO.equals(busServiceItem.getCarPackage())
                && (BusServiceItem.AUDITSTATUS_APPROVED.equals(busServiceItem.getAuditStatus())
                || BusServiceItem.AUDITSTATUS_REPLY.equals(busServiceItem.getAuditStatus()))) {
            //若状态是审批通过或者审批拒绝，我们需要将状态修改为初始化
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_INIT);
            throw new RuntimeException("已通过套餐禁止修改");
        }

        return busServiceItemMapper.updateBusServiceItem(busServiceItem);
    }

    /**
     * 批量删除服务项
     *
     * @param ids 需要删除的服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemByIds(Long[] ids) {
        return busServiceItemMapper.deleteBusServiceItemByIds(ids);
    }

    /**
     * 删除服务项信息
     *
     * @param id 服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemById(Long id) {
        return busServiceItemMapper.deleteBusServiceItemById(id);
    }

    /**
     * 上架
     * @param id
     * @return
     */
    @Override
    public int saleOn(Long id) {
        //1.参数合理化验证
        if(id==null){
            throw new RuntimeException("参数错误");
        }
        BusServiceItem busServiceItem = selectBusServiceItemById(id);
        if(busServiceItem==null){
            throw new RuntimeException("参数错误");
        }
        //  必须是下架的状态才能上架操作
        if(BusServiceItem.SALESTATUS_ON.equals(busServiceItem.getSaleStatus())){
            throw new RuntimeException("该服务已经上架，请勿重复操作");
        }
        // 状态判断 是套餐且不是待审核状态禁止上架
        if(BusServiceItem.CARPACKAGE_YES.equals(busServiceItem.getCarPackage()) //是套餐
           &&!BusServiceItem.AUDITSTATUS_APPROVED.equals(busServiceItem.getAuditStatus())) // 不是审核通过{
            throw new RuntimeException("套餐状态必须是审核通过才可以上架");
        return busServiceItemMapper.changeSaleStatus(id,BusServiceItem.SALESTATUS_ON);
    }

    /**
     * 下架
     * @param id
     * @return
     */
    @Override
    public int saleOff(Long id) {
        //1.参数合理化验证
        if(id==null){
            throw new RuntimeException("参数错误");
        }
        BusServiceItem busServiceItem = selectBusServiceItemById(id);
        if(busServiceItem==null){
            throw new RuntimeException("参数错误");
        }
        //  必须是上架的状态才能下架操作
        if(BusServiceItem.SALESTATUS_OFF.equals(busServiceItem.getSaleStatus())){
            throw new RuntimeException("该服务已经下架，请勿重复操作");
        }

        return busServiceItemMapper.changeSaleStatus(id,BusServiceItem.SALESTATUS_OFF);



    }
}
